#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/param.h"

#define MAX 1024
int main(int argc, char *argv[])
{
    int index;
    int len;
    char *p;
    char *argvs[MAXARG] = {'\0'};
    if (argc < 2)
    {
        printf("input error.\r\n");
        exit(0);
    }

    for (index = 0; index + 1 < argc; index++)
    {
        argvs[index] = argv[index + 1];
    }
    char buf[MAX] = {'\0'};
    if (read(0, buf, MAX) > 0)
    {
        len = strlen(buf);
        if (buf[len - 1] == '\n')
            buf[len - 1] = '\0';

        len = strlen(buf);
        p = buf;
        argvs[index] = p;
        index++;
        p++;
        while (*p != '\0')
        {
            if (*p == ' ')
            {
                *p = '\0';
                argvs[index++] = p + 1;
            }
            p++;
        }

        if (fork() == 0)
        {
            exec(argvs[0], &argvs[0]);
        }
    }
    wait(0);
    exit(0);
}
